local folder "C:\Users\mlevere\OneDrive - Mathematica\Documents\Projects\Nepal\"

clear all
set more off

use "`folder'/Data/women_endline_reg_data", clear

capt postclose tables

tempfile tables
postfile tables str30(compr sg varname) sglev sgnum ///
                 sg_T_mean sg_C_mean sg_T_vs_C sg_T_vs_C_se sg_T_vs_C_p  ///										 
				 overall_p sg_N_T sg_N_C using `tables', replace

// Income subgroup
sum income_total_b, detail
gen low_income = income_total_b < `r(p50)' if income_total_b ~= .

gen young_child = youngest_age_months_b <= 12 if !mi(youngest_age_months_b) & !inrange(youngest_age_months_b,25,36)

foreach sg in low_income young_child {
	levelsof `sg', clean local(sglevels)
	local n_sg : word count `sglevels'

foreach v in knowledge_index behav_index {
if "`v'" == "knowledge_index" local if ""
if "`v'" == "behav_index" local if " & woman_numkids_e > woman_numkids_b & youngest_age_months_e <= 13"
	svy: reg `v' info_vdc##i.`sg' cash_vdc##i.`sg' woman_age_b2-woman_age_b4 woman_weight_b woman_height_b school_b if flag == 0 `if'
	testparm i1.info_vdc#i.`sg'
	local overall_p_T1_vs_C = `r(p)'
	testparm i1.cash_vdc#i.`sg'
	local overall_p_T2_vs_C = `r(p)'

	margins, dydx(info_vdc cash_vdc) over(`sg') post
	mat results = r(table)

	  
      // build up test command based on number of levels in subgroup 
	  local testcmd
	  foreach lev of local sglevels {
        if "`testcmd'" == "" local testcmd    [1.info_vdc]`lev'.`sg' - [1.cash_vdc]`lev'.`sg'
		local testcmd             `testcmd' = [1.info_vdc]`lev'.`sg' - [1.cash_vdc]`lev'.`sg' 
      }	  
	  test `testcmd'
	  local overall_p_T1_vs_T2 = `r(p)'	
	  
	  local sgnum = 1
	  local col_T1_vs_C = 1*`n_sg' + 1
      local col_T2_vs_C = 3*`n_sg' + 1
		
	  foreach lev of local sglevels {
	    		
	    foreach compr in T1_vs_C T2_vs_C T1_vs_T2 { // must run in this order bec T1_vs_T2 means uses the previous means
	          
          if "`compr'" != "T1_vs_T2"	{  // the T1_vs_C and T2_vs_C stats are straight from regression		

		    local sg_T_vs_C    = results[1, `col_`compr'']
      	    local sg_T_vs_C_se = results[2, `col_`compr'']
      	    local sg_T_vs_C_p  = results[4, `col_`compr'']

      	    sum `v' if e(sample) & info_vdc == 0 & cash_vdc == 0 & `sg' == `lev' //, meanonly
      	    local sg_C_mean = r(mean)
		    local sg_T_mean_`compr' = `sg_C_mean' + `sg_T_vs_C'
		  
		    // Get sample sizes 	
            qui count if e(sample) & info_vdc == 0 & cash_vdc == 0 & `sg' == `lev'
            local sg_N_C = r(N)
            if "`compr'" == "T1_vs_C" qui count if e(sample) & info_vdc == 1  & `sg' == `lev'
		    if "`compr'" == "T2_vs_C" qui count if e(sample) & cash_vdc == 1  & `sg' == `lev'
            local sg_N_T_`compr' = r(N) 
		
      	  }
		  // T1 vs T2 comparison requires additional test ("C" = T2 in this comparison)
		  else {
		    lincom [1.info_vdc]`lev'.`sg' - [1.cash_vdc]`lev'.`sg'
		    local sg_T_vs_C    = r(estimate)
		    local sg_T_vs_C_se = r(se)
		    local sg_T_vs_C_p  = `r(p)'
		    
		    di abs(`sg_T_vs_C' - (`sg_T_mean_T1_vs_C' - `sg_T_mean_T2_vs_C')) 
		    local sg_C_mean         = `sg_T_mean_T2_vs_C'
		    local sg_T_mean_`compr' = `sg_T_mean_T1_vs_C'
               			  			  
		    // Sample sizes are same as above (recall: "C" stands for T2 in this comparison) 	
            local sg_N_C         = `sg_N_T_T2_vs_C'
            local sg_N_T_`compr' = `sg_N_T_T1_vs_C' 
		  }
		  
		    post tables ("`compr'") ("`sg'") ("`v'")  (`lev') (`sgnum')   ///
      	     (`sg_T_mean_`compr'') (`sg_C_mean') (`sg_T_vs_C') (`sg_T_vs_C_se') (`sg_T_vs_C_p')   ///
      	     (`overall_p_`compr'') (`sg_N_T_`compr'') (`sg_N_C')      		  
  	    }
		local ++sgnum       // increment counter for the subgroup local name
	    local ++col_T1_vs_C // increment counter for matrix column for each subgroup
		local ++col_T2_vs_C // increment counter for matrix column for each subgroup
	  }
}
}

use "`folder'/Data/child_endline_reg_data", clear

// Income subgroup
sum income_total_b if flag == 0 & !inrange(child_tot_months_b,25,36), detail
gen low_income = income_total_b < `r(p50)' if income_total_b ~= .

gen young_child = child_tot_months_b <= 12 if !inrange(child_tot_months_b,25,36)

foreach sg in low_income young_child {

	levelsof `sg', clean local(sglevels)
	local n_sg : word count `sglevels'

foreach v in anth_index asq_index {

if "`v'" == "anth_index" local controls "male child_tot_months2-child_tot_months20 school_e underweight_b underweight_no_b stunted_b stunted_no_b wasted_b wasted_no_b hh_head_male_e hh_head_age_e num_hh_members_e"
if "`v'" == "asq_index" local controls "male child_tot_months2-child_tot_months20 school_e underweight_b underweight_no_b stunted_b stunted_no_b wasted_b wasted_no_b hh_head_male_e hh_head_age_e num_hh_members_e enumerator_fe2-enumerator_fe36"
	svy: reg `v' info_vdc##i.`sg' cash_vdc##i.`sg' `controls' if flag == 0 & !inrange(child_tot_months_b,25,36)
	testparm i1.info_vdc#i.`sg'
	local overall_p_T1_vs_C = `r(p)'
	testparm i1.cash_vdc#i.`sg'
	local overall_p_T2_vs_C = `r(p)'

	margins, dydx(info_vdc cash_vdc) over(`sg') post
	mat results = r(table)

	  
      // build up test command based on number of levels in subgroup 
	  local testcmd
	  foreach lev of local sglevels {
        if "`testcmd'" == "" local testcmd    [1.info_vdc]`lev'.`sg' - [1.cash_vdc]`lev'.`sg'
		local testcmd             `testcmd' = [1.info_vdc]`lev'.`sg' - [1.cash_vdc]`lev'.`sg' 
      }	  
	  test `testcmd'
	  local overall_p_T1_vs_T2 = `r(p)'	
	  
	  local sgnum = 1
	  local col_T1_vs_C = 1*`n_sg' + 1
      local col_T2_vs_C = 3*`n_sg' + 1
		
	  foreach lev of local sglevels {
	    		
	    foreach compr in T1_vs_C T2_vs_C T1_vs_T2 { // must run in this order bec T1_vs_T2 means uses the previous means
	          
          if "`compr'" != "T1_vs_T2"	{  // the T1_vs_C and T2_vs_C stats are straight from regression		

		    local sg_T_vs_C    = results[1, `col_`compr'']
      	    local sg_T_vs_C_se = results[2, `col_`compr'']
      	    local sg_T_vs_C_p  = results[4, `col_`compr'']

      	    sum `v' if e(sample) & info_vdc == 0 & cash_vdc == 0 & `sg' == `lev' //, meanonly
      	    local sg_C_mean = r(mean)
		    local sg_T_mean_`compr' = `sg_C_mean' + `sg_T_vs_C'
		  
		    // Get sample sizes 	
            qui count if e(sample) & info_vdc == 0 & cash_vdc == 0 & `sg' == `lev'
            local sg_N_C = r(N)
            if "`compr'" == "T1_vs_C" qui count if e(sample) & info_vdc == 1  & `sg' == `lev'
		    if "`compr'" == "T2_vs_C" qui count if e(sample) & cash_vdc == 1  & `sg' == `lev'
            local sg_N_T_`compr' = r(N) 
		
      	  }
		  // T1 vs T2 comparison requires additional test ("C" = T2 in this comparison)
		  else {
		    lincom [1.info_vdc]`lev'.`sg' - [1.cash_vdc]`lev'.`sg'
		    local sg_T_vs_C    = r(estimate)
		    local sg_T_vs_C_se = r(se)
		    local sg_T_vs_C_p  = `r(p)'
		    
		    di abs(`sg_T_vs_C' - (`sg_T_mean_T1_vs_C' - `sg_T_mean_T2_vs_C')) 
		    local sg_C_mean         = `sg_T_mean_T2_vs_C'
		    local sg_T_mean_`compr' = `sg_T_mean_T1_vs_C'
               			  			  
		    // Sample sizes are same as above (recall: "C" stands for T2 in this comparison) 	
            local sg_N_C         = `sg_N_T_T2_vs_C'
            local sg_N_T_`compr' = `sg_N_T_T1_vs_C' 
		  }
		  
		    post tables ("`compr'") ("`sg'") ("`v'")  (`lev') (`sgnum')   ///
      	     (`sg_T_mean_`compr'') (`sg_C_mean') (`sg_T_vs_C') (`sg_T_vs_C_se') (`sg_T_vs_C_p')   ///
      	     (`overall_p_`compr'') (`sg_N_T_`compr'') (`sg_N_C')      		  
  	    }
		local ++sgnum       // increment counter for the subgroup local name
	    local ++col_T1_vs_C // increment counter for matrix column for each subgroup
		local ++col_T2_vs_C // increment counter for matrix column for each subgroup
	  }
}

}

	  postclose tables
	  use "`tables'", clear
	  
	  export excel using "`folder'/Output/TableA3_subgroup_impacts_income.xlsx" if sg == "low_income", firstrow(variables) replace
	  
	  export excel using "`folder'/Output/TableA4_subgroup_impacts_childage.xlsx" if sg == "young_child", firstrow(variables) replace
	  